package com.example.luke.shengdangxiaoattendance.Model;

import android.content.Context;

import com.example.luke.shengdangxiaoattendance.Database.DingHanDB;
import com.example.luke.shengdangxiaoattendance.Util.GlobalFunction;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by luke on 23/05/2017.
 */

public class Course {

    public static   final String DH_TABLE_NAME = "GoodList";

    public static   final String  course_Time="course_Time";//商品编号
    public static   final String  course_name_1="course_name_1";//商品编号
    public static   final String  course_name_2="course_name_2";//商品编号
    public static   final String  course_teacher="course_teacher";//商品编号
    public static   final String  course_class="course_class";//商品编号
    public static   final String  course_dateTime="course_dateTime";//商品编号

    public String Time;
    public String name1;
    public String name2;
    public String teacher;
    public String className;
    private  static  Course course;
    public ArrayList<Course >courseArrayList;
    public String dateTime;
    public  static boolean finishAllClass;

    public  static final String DH_CREATE_TABLE="CREATE TABLE " + DH_TABLE_NAME + "( " + course_Time + " TEXT," + course_name_1 + " TEXT,"
            + course_name_2 + " TEXT," +course_dateTime +" TEXT,"+ course_teacher + " TEXT," + course_class + " TEXT )";




    private Context context;


    public static Course getInstance(Context context){

        if (course==null){
            course=new Course();
            course.context=context;

        }
        return course;

    }

    public static void clearAllCourse(Context context,String date){

        DingHanDB.getInstance(context).clearAllCourse(date);

    }

    public static void storeToDB(Context context, Course course){



          DingHanDB.getInstance(context).insert(DH_TABLE_NAME,course);



    }

    public ArrayList<Course> getCourseArrayList() {

        return DingHanDB.getInstance(GlobalFunction.context).getAllCourseList();


    }
    boolean notifMark=true;

    public Course getCurrentCourse()  {

       // GlobalFunction.printTextByTag("casd213");

        courseArrayList=getCourseArrayList();
        Course temp = null;

        int lastCourseIndex;
        //GlobalFunction.printTextByTag("courseCount:"+courseArrayList.size());

        for (int i=0;i<courseArrayList.size();i++){

            Course course=courseArrayList.get(i);

            if (i==0 ){

            long minDiffer=GlobalFunction.getHourMinDifference(course.getStartTime(),new Date());

                if (minDiffer<90)//早上的课，一个半小时前，提前显示
                {

                    temp=course;

                }

            }


            if (course.Time.contains("-")) {
              //  GlobalFunction.printTextByTag("course"+course.Time);
//                GlobalFunction.printTextByTag(course.Time+":"+course.className);
                String[] timeInterval = course.Time.split("-");

                boolean stat =false;
                //GlobalFunction.isTimeBetweenTwoTime(timeInterval[0] + ":00", timeInterval[1] + ":00");
                if (stat) {
                    finishAllClass=false;
                    temp = course;


                }
                else {


                    int nextLesson=i+1;

                    if (new Date().after(course.getEndTime()) ){

                        if (nextLesson!=courseArrayList.size()-1){

                            if (nextLesson<courseArrayList.size()){

                                temp=courseArrayList.get(nextLesson);

                                finishAllClass=false;
                            }
                        }else

                        if (nextLesson==courseArrayList.size()-1){

                            if (nextLesson<courseArrayList.size()){

                                temp=courseArrayList.get(nextLesson);
                                finishAllClass=false;

                            }


                            if ( temp!=null && new Date().after(temp.getEndTime()) ){

                                temp=null;
                                GlobalFunction.printTextByTag("ccdd");
                                if (notifMark){
                                    GlobalFunction.ToastAlert(context,"已过今天所有课程");
                                    finishAllClass=true;
                                    notifMark=false;

                                }

                            }





                        }
                        else {

                            if (notifMark){
                                GlobalFunction.ToastAlert(context,"暂时没课");
                                notifMark=false;

                            }

                        }



                    }
                }
            }


        }

        return temp;

    }

    private boolean isMorning(){
        long time = System.currentTimeMillis();
        final Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);

        int hour = mCalendar.get(Calendar.HOUR);
        int apm = mCalendar.get(Calendar.AM_PM);

     //   GlobalFunction.printTextByTag(apm+"aa");
        return apm==0   ;

    }


    public Date getStartTime() {

        String[] timeInterval=  Time.split("-");

       String date= GlobalFunction.convertDate(new Date(),"yyyy-MM-dd");

        return GlobalFunction.convertStrToDate(date+" "+timeInterval[0],"yyyy-MM-dd HH:mm");

    }

    public Date getEndTime() {

        String[] timeInterval=  Time.split("-");

        String date= GlobalFunction.convertDate(new Date(),"yyyy-MM-dd");

        return GlobalFunction.convertStrToDate(date+" "+timeInterval[1],"yyyy-MM-dd HH:mm");

    }
}
